#coding:utf-8
__author__ = 'yangxianming'
import os
from common.koufuApi import KoufuApi
from common.koufuShop20Api import KoufuShop20Api
from common.tool import Tools
from common.KoufuConf import KoufuConf
import json
import requests


def case():
    #初始化必要的参数
    kou_fu_conf = KoufuConf()
    test_url = kou_fu_conf.shop20_domain
    kou_fu_shop_api = KoufuShop20Api()
    script_name = os.path.basename(__file__)
    tool = Tools
    tf = True
    test_result = ""

    #授权获得token
    p = kou_fu_shop_api.get_api_auth_koufu_authorize_data(update_body={"thirdUserId":"040351271326585810"})
    r = requests.post(test_url+kou_fu_shop_api.get_api_auth_koufu_authorize_data(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
    print("这个是接口api_auth_koufu_authorize返回的全部参数\n", r.content)
    token = r.json()["return_data"]["token"]
    kou_fu_conf.shop_headers.update({"X-Efulishop-Token":token})
    #获得商品列表
    p = kou_fu_shop_api.get_api_goods_product_list()
    r = requests.post(test_url+kou_fu_shop_api.get_api_goods_product_list(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
    # print("这个是接口api_goods_product_list返回的全部参数\n", r.json())
    goods_list = []
    test_result = r.json()
    for a in test_result["return_data"]:
       for aa in a:
           goods_list.append(aa["goodsName"])
    # print(goods_list)
    seach_list = []
    seach_list.append("券")
    for o in goods_list:
        seach_list.append(o)
    for one in seach_list:
        #循环查询商品
        p = kou_fu_shop_api.get_api_goods_es_product_list(update_body={"goodsName":one})
        print("#########参数########## ", p)
        r = requests.post(test_url + kou_fu_shop_api.get_api_goods_es_product_list(get_path=1), data=json.dumps(p), headers=kou_fu_conf.shop_headers)
        # print( r.json())
        r_list = r.json()

        if len(r_list["return_data"]["list"]) == 0:
            print("返回列表空:" , r_list)
            continue

        #比较返回数据
        # print(r_list["return_data"])
        print("返回数据近似度:")
        for one_item in r_list["return_data"]["list"]:
            goods_name = one_item["goodsName"]
            s = "[" + tool.comparison(None, one, goods_name) + "]" + goods_name
            print(s, end="")
        print("\n")



    #定义预期描述
    print(str(tf))
    # print(test_result)
    expect_result = "返回参数ebuyCode是否是空"

    #返回测试结果 第一个参数:True or False    第二个参数测试结果描述
    return tf, [test_result, expect_result]

case()